﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using ERP.Team.ProductionSystem;

namespace ERP.ERPSystem.DataAnalysis
{
    /// <summary>
    /// 数据分析-物料流转数据-grx
    /// </summary>
    public class MaterialflowdataService : ApplicationService, IMaterialflowdataService
    {
        private readonly IRepository<ProductionpickingModel, int> _productionpickingRepository;
        private readonly IRepository<ProductionWorkOrderMain, int> _productionWorkOrderRepository;

        public MaterialflowdataService(
            IRepository<ProductionpickingModel, int> productionpickingRepository,
            IRepository<ProductionWorkOrderMain, int> productionWorkOrderRepository)
        {
            _productionpickingRepository = productionpickingRepository;
            _productionWorkOrderRepository = productionWorkOrderRepository;
        }

        /// <summary>
        /// 获取物料流转柱状图数据
        /// </summary>
        /// <returns>柱状图数据</returns>
        public async Task<Dictionary<string, decimal>> GetMaterialFlowBarChartData()
        {
            // 获取所有生产领料数据
            var productionPickingList = await _productionpickingRepository.GetListAsync();

            // 按IssuanceOrderName分组，统计每个分组的TotalSum
            var barChartData = productionPickingList
                .GroupBy(p => p.IssuanceOrderName)
                .ToDictionary(
                    g => g.Key,
                    g => g.Sum(p => p.TotalSum)
                );

            return barChartData;
        }

        /// <summary>
        /// 获取物料流转折线图数据
        /// </summary>
        /// <returns>折线图数据</returns>
        public async Task<Dictionary<string, decimal>> GetMaterialFlowLineChartData()
        {
            // 获取所有生产领料数据
            var productionPickingList = await _productionpickingRepository.GetListAsync();

            // 按IssuanceOrderName分组，统计每个分组的TotalSum（与柱状图数据相同）
            var lineChartData = productionPickingList
                .GroupBy(p => p.IssuanceOrderName)
                .ToDictionary(
                    g => g.Key,
                    g => g.Sum(p => p.TotalSum)
                );

            return lineChartData;
        }

        /// <summary>
        /// 获取物料流转饼状图数据
        /// </summary>
        /// <returns>饼状图数据（包含名称、数值和百分比）</returns>
        public async Task<object> GetMaterialFlowPieChartData()
        {
            // 获取所有生产领料数据
            var productionPickingList = await _productionpickingRepository.GetListAsync();

            // 按IssuanceOrderName分组，统计每个分组的TotalSum
            var groupedData = productionPickingList
                .GroupBy(p => p.IssuanceOrderName)
                .Select(g => new
                {
                    Name = g.Key,
                    Value = g.Sum(p => p.TotalSum)
                })
                .ToList();

            // 计算总和
            var totalSum = groupedData.Sum(x => x.Value);

            // 构造饼状图数据，包含百分比
            var pieChartData = groupedData.Select(item => new
            {
                name = item.Name,
                value = item.Value,
                percentage = totalSum > 0 ? Math.Round((item.Value / totalSum) * 100, 2) : 0
            }).ToList();

            return new
            {
                data = pieChartData,
                total = totalSum
            };
        }

        /// <summary>
        /// 获取产品生产柱状图数据
        /// </summary>
        /// <returns>柱状图数据</returns>
        public async Task<Dictionary<string, decimal>> GetProductionWorkOrderBarChartData()
        {
            // 获取所有生产工单数据
            var workOrderList = await _productionWorkOrderRepository.GetListAsync();

            // 按WorkOrderName分组，统计每个分组的WorkOrderQuantity
            var barChartData = workOrderList
                .GroupBy(w => w.WorkOrderName)
                .ToDictionary(
                    g => g.Key,
                    g => (decimal)g.Sum(w => w.WorkOrderQuantity)
                );

            return barChartData;
        }

        /// <summary>
        /// 获取产品生产折线图数据
        /// </summary>
        /// <returns>折线图数据</returns>
        public async Task<Dictionary<string, decimal>> GetProductionWorkOrderLineChartData()
        {
            // 获取所有生产工单数据
            var workOrderList = await _productionWorkOrderRepository.GetListAsync();

            // 按WorkOrderName分组，统计每个分组的WorkOrderQuantity（与柱状图数据相同）
            var lineChartData = workOrderList
                .GroupBy(w => w.WorkOrderName)
                .ToDictionary(
                    g => g.Key,
                    g => (decimal)g.Sum(w => w.WorkOrderQuantity)
                );

            return lineChartData;
        }

        /// <summary>
        /// 获取产品生产饼状图数据
        /// </summary>
        /// <returns>饼状图数据（包含名称、数值和百分比）</returns>
        public async Task<object> GetProductionWorkOrderPieChartData()
        {
            // 获取所有生产工单数据
            var workOrderList = await _productionWorkOrderRepository.GetListAsync();

            // 按WorkOrderName分组，统计每个分组的WorkOrderQuantity
            var groupedData = workOrderList
                .GroupBy(w => w.WorkOrderName)
                .Select(g => new
                {
                    Name = g.Key,
                    Value = g.Sum(w => w.WorkOrderQuantity)
                })
                .ToList();

            // 计算总和
            var totalSum = groupedData.Sum(x => x.Value);

            // 构造饼状图数据，包含百分比
            var pieChartData = groupedData.Select(item => new
            {
                name = item.Name,
                value = item.Value,
                percentage = totalSum > 0 ? Math.Round(((decimal)item.Value / totalSum) * 100, 2) : 0
            }).ToList();

            return new
            {
                data = pieChartData,
                total = totalSum
            };
        }
    }
}
